using System;
using System.Data;
using System.Windows.Forms;
using BLL;
using Common;
using Model;

namespace UI {
    public partial class FrmUpdateMember : Form {
        private MemberBLL _memberBll = MemberBLL.GetMemberBll();
        public FrmUpdateMember() {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }

        private void FrmUpdateMember_FormClosing(object sender, FormClosingEventArgs e) {
        }

        public void InitMemberInfo(Member member) {
            Console.WriteLine("---------");
            InitMemberType();
            
            txtMid.Text = member.Mid.ToString();
            txtMname.Text = member.Mname;
            txtMoney.Text = member.Money.ToString();
            txtDisount.Text = member.Discount.ToString();
            txtPoints.Text = member.Points.ToString();
            txtPhoneNum.Text = member.PhoneNum;
            registDate.Value = member.RegistDate;
            endDate.Value = member.EndDate;
            birDate.Value = member.Birthday;

            cmbMemType.SelectedValue = member.Type.ToString();

            if (member.Gender == "男") {
                rdoMan.Checked = true;
            } else {
                rdoWomen.Checked = true;
            }
        }

        private void InitMemberType() {
            DataTable dataTable = _memberBll.QueryMemberType();
            cmbMemType.DisplayMember = "tname";
            cmbMemType.ValueMember = "tid";
            cmbMemType.DataSource = dataTable;
        }

        private void btnOk_Click(object sender, EventArgs e) {
            Result<Member> result = _memberBll.UpdateMember(new Member() {
                Mid = int.Parse(txtMid.Text.ToString()),
                Mname = txtMname.Text,
                Gender = rdoMan.Checked ? "男" : "女",
                Type = int.Parse(cmbMemType.SelectedValue.ToString()),
                Discount = double.Parse(txtDisount.Text),
                Money = double.Parse(txtMoney.Text),
                Points = int.Parse(txtPoints.Text),
                Birthday = birDate.Value,
                PhoneNum = txtPhoneNum.Text,
                RegistDate = registDate.Value,
                EndDate = endDate.Value
            });
            
            MessageBox.Show(result.Message);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}